<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="站点" prop="groupId">
        <el-input v-model="queryParams.groupId" placeholder="请输入分组id" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['iot:maintenance:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['iot:maintenance:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['iot:maintenance:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['iot:maintenance:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="maintenanceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="站点" align="center" prop="groupName" width="300" />
      <el-table-column label="运维人员" align="center" prop="maintenanceUser" />
      <el-table-column label="运维时间" align="center" prop="createTime" width="160" />
      <el-table-column label="整体运维" align="center" prop="maintenanceContent" />
      <el-table-column label="风机运维" align="fanContent">
        <template slot-scope="scope">
          <div>
            <div :key="index" v-for="(item, index) of arrayJson(scope.row.fanContent)">{{ item.fanContent }}---{{
              item.maintenanceContent }}</div>
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['iot:maintenance:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['iot:maintenance:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改设备运维对话框 -->
    <el-drawer :title="title" :visible.sync="open" direction="rtl" size="30%" :before-close="cancel">
      <el-form ref="form" :model="form" :rules="rules" label-width="auto" style="margin-bottom:30px;padding:20px">

        <el-form-item label="站点" prop="groupId">
          <el-select style="width: 100%;" v-model="form.groupId" placeholder="请选择站点">
            <el-option v-for="item in groupList" :key="item.groupId" :label="item.groupName" :value="item.groupId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="运维人员" prop="maintenanceUser">
          <el-input v-model="form.maintenanceUser" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="运维时间">
          <el-date-picker value-format="yyyy-MM-dd HH:mm:ss" style="width: 100%;" v-model="form.createTime"
            type="datetime" placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="整体运维">
          <el-input style="margin-top: 10px;" v-model="form.maintenanceContent" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="风机运维">
          <div @click="fanContentArrayAdd" style="text-align: right;width: 100%;color: green;cursor: pointer;">新增运维内容
          </div>
        </el-form-item>
        <el-form-item>
          <div class="fanContentCss" :key="index" v-for="(item, index) of fanContentArray">
            <div class="fanContentCssLeft">
              <el-select style="width: 100%;" v-model="item.fanContent" placeholder="请选择风机" clearable>
                <el-option v-for="dict in dict.type.maintenance_fan_device" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
              <el-input style="margin-top: 10px;" v-model="item.maintenanceContent" type="textarea"
                placeholder="请输入内容" />
            </div>
            <div @click="fanContentArrayDel(index)" class="fanContentCssRight">
              删除
            </div>
          </div>
        </el-form-item>
      </el-form>

      <!-- 底部按钮区域 -->
      <div class="drawer-footer">
        <el-button @click="open = false">取消</el-button>
        <el-button type="primary" @click="submitForm">保存</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { listMaintenance, getMaintenance, delMaintenance, addMaintenance, updateMaintenance } from "@/api/iot/maintenance";
import { allListGroup } from '@/api/iot/group'
export default {
  name: "Maintenance",
  dicts: ['maintenance_fan_device'],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备运维表格数据
      maintenanceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        groupId: null,
        maintenanceUser: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      fanContentArray: [{
        fanContent: null,
        maintenanceContent: null,
      }],
      groupList: [],
    };
  },
  created() {
    this.getList();
    this.allListGroupFun();
  },
  methods: {
    arrayJson(item) {
      return JSON.parse(item);
    },
    allListGroupFun() {
      allListGroup().then(response => {
        this.groupList = response.data;
      });
    },
    /** 查询设备运维列表 */
    getList() {
      this.loading = true;
      listMaintenance(this.queryParams).then(response => {
        this.maintenanceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        maintenanceId: null,
        groupId: null,
        userId: null,
        userName: null,
        maintenanceUser: null,
        fanContent: null,
        maintenanceContent: null,
        extendData: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.maintenanceId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备运维";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const maintenanceId = row.maintenanceId || this.ids
      getMaintenance(maintenanceId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备运维";
        this.fanContentArray = JSON.parse(this.form.fanContent);
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.fanContent = JSON.stringify(this.fanContentArray);
          if (this.form.maintenanceId != null) {
            updateMaintenance(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMaintenance(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const maintenanceIds = row.maintenanceId || this.ids;
      this.$modal.confirm('是否确认删除设备运维编号为"' + maintenanceIds + '"的数据项？').then(function () {
        return delMaintenance(maintenanceIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('iot/maintenance/export', {
        ...this.queryParams
      }, `maintenance_${new Date().getTime()}.xlsx`)
    },
    fanContentArrayAdd() {
      this.fanContentArray.push({
        fanContent: null,
        maintenanceContent: null,
      })
    },
    fanContentArrayDel(index) {
      if (this.fanContentArray.length === 1) {
        this.$modal.msgError("至少保留一条数据");
        return;
      }
      this.fanContentArray.splice(index, 1)
    }
  }
};
</script>

<style scoped lang="scss">
.drawer-content {
  padding: 20px;
}

.drawer-footer {
  margin-top: 20px;
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 2px 35px;
  border-top: 1px solid #e8e8e8;
  text-align: right;
  background: #fff;
}

.drawer-footer button {
  margin-left: 10px;
}

.information_warp {
  border-radius: 12px 12px 12px 12px;
  margin-bottom: 10px;

  .clearfix {
    padding: 10px 40px;
    border-bottom: 1px solid gainsboro;
    margin-bottom: 10px;
  }

  .information_title {
    // border-radius: 12px 12px 0px 0px;
    font-family: PingFang SC, PingFang SC;
    font-weight: 800;
    font-size: 18px;
    line-height: 23px;
    padding: 6px 10px;
  }

  .information_text {
    font-family: PingFang SC, PingFang SC;
    font-weight: 500;
    font-size: 16px;
    color: #EDA701;
    margin: 5px 0;
  }

  .information_ur {
    margin-bottom: 5px;
    display: flex;

    .information_bit {
      width: 80px;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .information_select {
      width: 100px;
      margin-right: 5px;
    }

    .information_li {
      flex: 1;

    }

    .information_del {
      width: 50px;
      display: flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;
    }
  }


}

.fanContentCss {
  display: flex;
  width: 100%;
  justify-content: space-between;
}

.fanContentCssLeft {
  flex: 1;
}

.fanContentCssRight {
  width: 100px;
  display: flex;
  justify-content: center;
  align-items: center;
  color: red;
  cursor: pointer;
}
</style>
